package com.atguigu.interceptor;

import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.util.WebUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断是否登录
        UserInfo existUser = (UserInfo) request.getSession().getAttribute("existUser");
        if(existUser == null) {
            //说明没有登陆,
            // 因为follow.html的分页查询数据需要一个208数据,所以这里需要传一个208,且他需要一个json字符串.所以这里需要转成json字符串
            WebUtil.writeJSON(response, Result.build(null, ResultCodeEnum.LOGIN_AUTH));
            return false;
        }

        return true;
    }
}
